# coding: utf-8
import json
import os

from flask import Flask, request

from air_web.config.config import app_conf, basedir, config
from air_web.web_flask.common.constants import Status, StatusMsg
from air_web.web_flask.common.extensions import cors
from air_web.web_flask.common.logger import init_log
from air_web.web_flask.common.tools import get_default_result


def create_app():
    """初始化flask app"""
    app = Flask("gate")
    app.config.from_object(app_conf)
    register_extensions(app)
    register_logger()
    write_es_auth_to_env()
    return app


def write_es_auth_to_env():
    """es用户名密码写入环境变量，ruleng会使用"""
    es_auth = {"http_auth": (config["ES_USER"], config["ES_PASSWORD"])}
    os.environ.update({"ZS_ES_EXTRA_ARGS": json.dumps(es_auth)})


def register_logger():
    """初始化日志"""
    docker_log_path = "/home/zshield/logs/"
    if os.path.exists(docker_log_path):
        init_log(docker_log_path)
    else:
        my_log_path = os.path.join(basedir, "logs")
        if not os.path.exists(my_log_path):
            os.mkdirs(docker_log_path)
        init_log(my_log_path)


def register_extensions(app):
    """初始化flask拓展工具"""
    # cors.init_app(app)
    pass
